<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_search
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

$helper = JPATH_COMPONENT.'/helpers/glycophile.php';
require_once($helper);

/**
 *
 * @package		Joomla.Site
 * @subpackage	com_glycophile
 * @since 1.5
 */
class GlycophileModelNominatifs extends JModelLegacy
{
	/**
	 * data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	/**
	 * Helper
	 *
	 * @var object
	 */
	var $_helper = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();

		//Get configuration
		$app	= JFactory::getApplication();
		$config = JFactory::getConfig();
		
		$this->_helper = new GlycophileHelper();

		// Get the pagination request variables
		$this->setState('limit', $app->getUserStateFromRequest('com_search.limit', 'limit', $config->get('list_limit'), 'uint'));
		$this->setState('limitstart', JRequest::getUInt('limitstart', 0));
    
    
		$this->countAllRows();
		$this->getList();
	}

	/**
	 * Method to get Series list
	 * Si theme sélectionné  (exemple Sujet divers id = 16 )
	 *
	 * @access public
	 * @return array
	 */
	function getData()
	{
		return $this->_data;
	}
	
	function getTotal()
	{
		return $this->_total;
	}
	
	function getPagination()
	{
		return $this->_pagination;
	}
	
	function getQuery($id_pays = 0, $limit=0){

    $nominatif = JRequest::getVar('nominatif', null);
    $typeSucre = JRequest::getVar('typeSucre', null);

    $type = JRequest::getVar('type', 0);


    
    $query = "SELECT N. * , Count( N.id ) AS CPT,N.id as identN, T.tome, T.rang as rang, C.classement, R.raffinerie
              FROM #__glycophile_nominatif as N, #__glycophile_tome as T, #__glycophile_classement as C,
              #__glycophile_raffinerie as R
              where T.id = N.IdentTome AND N.IdentClassement = C.id and N.IdentRaffinerie = R.id ";
              $query .= $id_pays != 0 ? " AND N.Identpays  = ". $id_pays : '' ;
              $query .= $nominatif  ? " AND N.Code  = '". $nominatif."'" : '' ;
              $query .= $nominatif  ? '' : ' AND N.`NombreNominatif` > 0' ;
             if($type == 1){
                $query .= " AND T.typeSucre <> 'E'";
              } else {
                $query .= " AND T.typeSucre = 'E'";

              }
              //$query .= $typeSucre  ? " AND T.typeSucre ='".$typeSucre."'" : '';
              $query .= " GROUP BY N.`Designation`";
              $query .= $nominatif  ? 'ORDER BY N.`Designation`' : ' ORDER BY N.`DateCreation` DESC' ;
          //    $query .= ' ORDER BY N.`DateCreation` DESC';
//              if($nominatif){
//                
//              } else {
//                $query .= $limit != 0 ? " limit 0, $limit" : '' ;
//              }

    // echo $query;
    return $query ;
	}
	
	function countAllRows(){
		$db = $this->getDbo();
		 $type = JRequest::getVar('type', 0);
    if($type == 0){
      $id_pays = JRequest::getVar('pays', 0);
    } else {
        $id_pays = 1;
    }
		$query = $this->getQuery($id_pays);
		$db->setQuery($query);
		$items = $db->query();
		$this->_total = $db->getNumRows();
	}
	
	function getList(){
		$db = $this->getDbo();
		global $mainframe;
		
		$config = JFactory::getConfig();
		// Get the pagination request variables
		
    $type = JRequest::getVar('type', 0);
    if($type == 0){
      $id_pays = JRequest::getVar('pays', 0);
    } else {
        $id_pays = 1;
    }
    
    $nominatif = JRequest::getVar('nominatif', null);
     if($nominatif){
        $limitstart = JRequest::getUInt('limitstart', 1);
        $limit = $config->getValue('config.list_limit');
     } else {
       $limitstart = 1;
       $limit = JRequest::getVar('limit', 1);
     }
     
    

		$ligne = ($limitstart - 1) * $limit;
    
		$query = $this->getQuery($id_pays, $limit);
    $query .= " Limit ". $ligne .", ". $limit;
	//	echo $query;
		$db->setQuery($query);
		$series = $db->loadObjectList();
		
		foreach($series as $serie){
			$query = "Select * From #__glycophile_sousserie ss";
			$query .= "	Where id_serie = ".$serie->id;
			$db->setQuery($query);
			$sseries = $db->loadObjectList();
			$serie->sseries = $sseries;
		}
		
		$this->_data = $series;

    $Itemid = JRequest::getVar('Itemid', 0);
		$url = 'index.php?option=com_glycophile&view=nominatifs&pays='.$id_pays.'&type='. $type .'&nominatif='.$nominatif.'&Itemid='.$Itemid;
		//$url .= '&orderBy='. $orderBy .'&order='.$order;
		$this->_pagination = $this->_helper->getPaginationBar($this->_total, $limit, $url);

	}

}
